/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.ktmall.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.ktmall.entity.NewsType;
import com.ktjiaoyu.ktmall.service.NewsTypeService;
import com.ktjiaoyu.ktmall.service.impl.NewsTypeServiceImpl;
import com.ktjiaoyu.ktmall.utils.Page;
import com.ktjiaoyu.ktmall.utils.R;
import com.ktjiaoyu.ktmall.utils.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * NewsType业务控制器
 * @author 科泰集团教研团队
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 * @version 1.0.0
 */
@WebServlet(name = "NewsTypeServlet", urlPatterns = "/api/newsType")
@MultipartConfig(maxFileSize = 1024 * 1024 * 50) // 最多传50M
public class NewsTypeServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String opr = request.getParameter("opr");
        NewsTypeService newsTypeService = new NewsTypeServiceImpl();
        // 获取所有前端传来的数据，为null也没关系
        Long id = RequestUtil.parseLong(request, "id", null);
        String typeName = request.getParameter("typeName");
        String remark = request.getParameter("remark");
        // 构建对象，方便后续使用
        NewsType newsType = new NewsType();
        newsType.setId(id);
        newsType.setTypeName(typeName);
        newsType.setRemark(remark);
        // 返回的json对象
        R result = null;
        if("page".equals(opr)){
            // 分页查询
            Integer pageNo = RequestUtil.parseInt(request, "pageNo", 1);
            Integer pageSize = RequestUtil.parseInt(request, "pageSize", 10);
            Page<NewsType> page = newsTypeService.findPage(newsType, pageNo, pageSize);
            // 返回分页数据
            result = R.ok(page);
        }else if("list".equals(opr)){
            // 列表全部数据
            List<NewsType> list = newsTypeService.findAll();
            // 返回所有数据
            result = R.ok(list);
        }else if("save".equals(opr)){
            // 新增或修改数据，统一使用save
            boolean save = false;
            try {
                save = newsTypeService.saveOrUpdate(newsType);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            if(save){
                result = R.ok(newsType);
            }else{
                result = R.fail(newsType);
            }
        }else if("detail".equals(opr)){
            // 获取详情数据
            newsType = newsTypeService.findById(id);
            if(newsType != null){
                result = R.ok(newsType);
            }else{
                result = R.fail();
            }
        }else if("del".equals(opr)){
            // 删除单条数据
            boolean deleted = newsTypeService.removeById(id);
            if(deleted){
                result = R.ok(id);
            }else{
                result = R.fail(id);
            }
        }else if("delBatch".equals(opr)){
            // 批量删除数据
            Long[] ids = RequestUtil.parseLongValues(request, "ids", null);
            boolean deleted = newsTypeService.removeBatch(ids);
            if(deleted){
                result = R.ok(ids);
            }else{
                result = R.fail(ids);
            }
        }else if("importBatch".equals(opr)){
            // 批量导入数据
            boolean imported = false;
            try {
                List<NewsType> newsTypeList = RequestUtil.readImportFile(request, "importFile", NewsType.class  );

                imported = newsTypeService.importBatch(newsTypeList.toArray(new NewsType[newsTypeList.size()]));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(imported){
                result = R.ok();
            }else{
                result = R.fail();
            }
        }
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
